import Vue from 'vue'
import $ from 'jquery'
import jquery from 'jquery'

import validation from 'jquery-validation'
import axios from 'axios'
import Mint from 'mint-ui';
import 'mint-ui/lib/style.css';
import router from './router/index.js'
import MuseUI from 'muse-ui';
import 'muse-ui/dist/muse-ui.css';
import 'muse-ui-message/dist/muse-ui-message.css';
import Message from 'muse-ui-message';
import Toast from 'muse-ui-toast';
import '../public/alifont/iconfont.css'

import '../public/common.css'
import '../public/common.js'
import './assets/element'
import layer from 'layui-layer'

import {$Api, $ResponseCode, $Dictionary} from './app.js'
import {Toast as MintToast, MessageBox} from 'mint-ui';
Vue.use(Toast);
Vue.use(Message);
Vue.use(MuseUI);
Vue.use(Mint);
Vue.config.productionTip = false;
Vue.prototype.$Api = $Api;
Vue.prototype.$ResponseCode = $ResponseCode;
Vue.prototype.$Dictionary = $Dictionary;
Vue.prototype.$http = axios;

// new Vue({
//   render: h => h(App),
//     router,
// }).$mount('#app')
Vue.prototype.logining = 0;
/**
 * 请求拦截
 */
axios.interceptors.request.use(function (config) {
    // 在发起请求请做一些业务处理
    return config;
}, function (error) {
    // 对请求失败做处理
    console.log("数据请求失败");
    console.log(error);
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    // if (response.data.code != $ResponseCode.ok) {
    //     if (response.data.code== $ResponseCode.noLogin) {
    //         // 密码错误情况下再次弹出验证
    //         //login();
    //     }else {
    //         window.failedNotify(vm, response.data.info.msg);
    //     }
    //
    // }

    return response;
}, function (error) {
    // 对响应错误做点什么
    console.log("数据响应失败");
    console.log(error.response);
    var response = error.response;
    if (response.status == $ResponseCode.noLogin) {
        if (vm.logining ==0) {
            window.login();
        }
    }
    return Promise.reject(error);
});

window.login = function () {
    vm.logining=1;
    vm.$prompt('请输入密码', '密码', {
        validator(value) {
            return {
                valid: /[^(undefined)].+/.test(value),
                message: '请输入密码',

            }
        },
        okLabel: "登陆",
        inputType: "password"
    }).then(({result, value}) => {
        if (result) {
            var formData = new FormData();
            formData.append('password', value);
            axios.post($Api.basic.login, formData).then(function (res) {
                var resp = res.data;
                if (resp.code == $ResponseCode.ok) {
                    window.successNotify(MintToast,  "登陆成功！")
                    window.location.reload();
                } else {
                    login();
                }
            })
        }
        vm.logining = 0;
    });

};



var vm = new Vue({
    el: "#app",
    router
});

